package com.bx.implatform.growUp.service.Impl;


import com.bx.implatform.enums.ResultCode;
import com.bx.implatform.growUp.config.GrowUpProperties;
import com.bx.implatform.growUp.exception.GrowUpException;
import com.bx.implatform.growUp.param.*;
import com.bx.implatform.growUp.service.GrowUpApiService;
import com.bx.implatform.growUp.utils.GrowUpExternalApiService;
import com.bx.implatform.kafka.entity.PublishApproveChange;
import com.bx.implatform.session.SessionContext;
import com.bx.implatform.util.ConvertNumbers;
import com.fasterxml.jackson.databind.JsonNode;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor
public class GrowUpApiServiceImpl implements GrowUpApiService {

    @Resource
    private GrowUpExternalApiService externalApiService;
    @Resource
    private GrowUpProperties growUpProperties;

    private String buildUrl(String path) {
        return growUpProperties.getUrl() + path;
    }


    /**
     * 路线列表
     *
     * @return
     */
    @Override
    public JsonNode getRoads() {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/getRoads");
            log.debug("长大路段列表接口: {}", url);
            JsonNode response = externalApiService.post(url, null, JsonNode.class);
            return ConvertNumbers.convertNumbersToString(response);
        } catch (GrowUpException e) {
            log.error("长大路段列表接口: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "长大路段列表接口", e.getMessage());
        }
    }

    /**
     * 事件类型列表
     *
     * @return
     */
    @Override
    public JsonNode getEventTypeList() {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/getEventTypeList");
            log.debug("事件类型列表接口: {}", url);
            JsonNode response = externalApiService.post(url, null, JsonNode.class);
            return ConvertNumbers.convertNumbersToString(response);
        } catch (GrowUpException e) {
            log.error("事件类型列表接口: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "事件类型列表接口", e.getMessage());
        }
    }

    /**
     * 事件类别列表
     *
     * @return
     */
    @Override
    public JsonNode getEventCategoryList() {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/getEventCategoryList");
            log.debug("长大事件类别列表接口: {}", url);
            JsonNode response = externalApiService.post(url, null, JsonNode.class);
            return ConvertNumbers.convertNumbersToString(response);
        } catch (GrowUpException e) {
            log.error("长大事件类别列表接口: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "长大事件类别列表接口查询失败", e.getMessage());
        }
    }

    /**
     * 事件列表
     *
     * @return
     */
    @Override
    public JsonNode getEventList(@Valid GetEventListParam param, String userId) {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/getEventList");
            log.debug("长大事件列表接口: {}", url);
            JsonNode response = externalApiService.post(url, param, JsonNode.class, StringUtils.isEmpty(userId) ? SessionContext.getSession().getUserId() : userId);
            return ConvertNumbers.convertNumbersToString(response);
        } catch (GrowUpException e) {
            log.error("长大事件列表接口: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "长大事件列表接口查询失败", e.getMessage());
        }
    }

    /**
     * 事件动态信息
     *
     * @param eventId
     * @return
     */
    @Override
    public JsonNode getEventDynamicInfoList(String eventId) {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/getEventDynamicInfoList");
            log.debug("事件动态信息: {}", url);
            Map<Object, Object> paramMap = new HashMap<>();
            paramMap.put("eventId", eventId);
            JsonNode response = externalApiService.post(url, paramMap, JsonNode.class, SessionContext.getSession().getUserId());
            return ConvertNumbers.convertNumbersToString(response);
        } catch (GrowUpException e) {
            log.error("事件动态信息: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "事件动态信息查询失败", e.getMessage());
        }
    }

    /**
     * 上传文件
     *
     * @param file
     * @return
     */
    @Override
    public JsonNode uploadFile(MultipartFile file) {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/uploadFile");
            log.debug("上传文件: {}", url);
            MultiValueMap<String, Object> multipartRequest = new LinkedMultiValueMap<>();
            // 添加文件和参数到 multipartRequest
            multipartRequest.add("file", file.getResource());
            JsonNode response = externalApiService.post(url, multipartRequest, JsonNode.class);
            return ConvertNumbers.convertNumbersToString(response);
        } catch (GrowUpException e) {
            log.error("上传文件: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "上传文件失败", e.getMessage());
        }
    }

    /**
     * 保存事件进度
     *
     * @param param
     * @return
     */
    @Override
    public JsonNode saveEventProgress(SaveEventProgressParam param) {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/saveEventProgress");
            log.debug("保存事件进度: {}", url);
            JsonNode response = externalApiService.post(url, param, JsonNode.class);
            return ConvertNumbers.convertNumbersToString(response);
        } catch (GrowUpException e) {
            log.error("保存事件进度: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "长大保存事件进度失败", e.getMessage());
        }
    }

    /**
     * 获取事件进度列表
     *
     * @param eventId
     * @return
     */
    @Override
    public JsonNode getEventProgressList(String eventId) {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/saveEventProgress");
            log.debug("获取事件进度列表: {}", url);
            Map<Object, Object> paramMap = new HashMap<>();
            paramMap.put("eventId", eventId);
            JsonNode response = externalApiService.post(url, paramMap, JsonNode.class);
            return ConvertNumbers.convertNumbersToString(response);
        } catch (GrowUpException e) {
            log.error("获取事件进度列表: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "长大获取事件进度列表失败", e.getMessage());
        }
    }

    /**
     * 获取事件统计
     *
     * @param param
     * @return
     */
    @Override
    public JsonNode getEventTypeStatistics(GetEventTypeStatisticsParam param) {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/getEventTypeStatistics");
            log.debug("获取事件统计: {}", url);
            JsonNode response = externalApiService.post(url, param, JsonNode.class,SessionContext.getSession().getUserId());
            return ConvertNumbers.convertNumbersToString(response);
        } catch (GrowUpException e) {
            log.error("获取事件统计: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "长大获取事件统计失败", e.getMessage());
        }
    }

    /**
     * 获取告警类型分组
     *
     * @return
     */
    @Override
    public JsonNode getAlarmTypeGroup() {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/getAlarmTypeGroup");
            log.debug("获取告警类型分组: {}", url);
            JsonNode response = externalApiService.post(url, null, JsonNode.class);
            return ConvertNumbers.convertNumbersToString(response);
        } catch (GrowUpException e) {
            log.error("获取告警类型分组: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "长大获取告警类型分组接口失败", e.getMessage());
        }
    }

    /**
     * 获取告警类型
     *
     * @return
     */
    @Override
    public JsonNode getAlarmTypeList() {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/getAlarmTypeList");
            log.debug("获取告警类型: {}", url);
            JsonNode response = externalApiService.post(url, null, JsonNode.class);
            return ConvertNumbers.convertNumbersToString(response);
        } catch (GrowUpException e) {
            log.error("获取告警类型: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "长大获取告警类型接口失败", e.getMessage());
        }
    }

    /**
     * 获取告警列表
     *
     * @param param
     * @return
     */
    @Override
    public JsonNode getAlarmList(GetAlarmListParam param) {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/getAlarmList");
            log.debug("获取告警列表: {}", url);
            JsonNode response = externalApiService.post(url, param, JsonNode.class,SessionContext.getSession().getUserId());
            return ConvertNumbers.convertNumbersToString(response);
        } catch (GrowUpException e) {
            log.error("获取告警列表: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "长大获取告警列表接口失败", e.getMessage());
        }
    }

    /**
     * 上报告警
     *
     * @param param
     * @return
     */
    @Override
    public JsonNode saveAlarm(SaveAlarmParam param) {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/saveAlarm");
            log.debug("上报告警: {}", url);
            JsonNode response = externalApiService.post(url, param, JsonNode.class);
            return ConvertNumbers.convertNumbersToString(response);
        } catch (GrowUpException e) {
            log.error("上报告警: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "长大上报告警接口失败", e.getMessage());
        }
    }

    /**
     * 获取路况列表
     *
     * @return
     */
    @Override
    public JsonNode getTrafficStateList() {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/getTrafficStateList");
            log.debug("获取路况列表: {}", url);
            JsonNode response = externalApiService.post(url, null, JsonNode.class);
            return response;
            // return ConvertNumbers.convertNumbersToString(response);
        } catch (GrowUpException e) {
            log.error("获取路况列表: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "长大获取路况列表接口失败", e.getMessage());
        }
    }

    /**
     * 获取事件分流路线记录
     *
     * @param eventId
     * @param id
     * @return
     */
    @Override
    public JsonNode getEventDivideRoute(String eventId, String id) {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/getEventDivideRoute");
            log.debug("获取事件分流路线记录: {}", url);
            Map<Object, Object> paramMap = new HashMap<>();
            paramMap.put("eventId", eventId);
            paramMap.put("id", id);
            JsonNode response = externalApiService.post(url, paramMap, JsonNode.class,SessionContext.getSession().getUserId());
            return ConvertNumbers.convertNumbersToString(response);
        } catch (GrowUpException e) {
            log.error("获取事件分流路线记录: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "长大获取事件分流路线记录失败", e.getMessage());
        }
    }

    /**
     * 获取摄像机视频播放地址
     *
     * @param deviceId
     * @param videoType
     * @return
     */
    @Override
    public String getVideoPlayUrl(String deviceId, String videoType) {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/getVideoPlayUrl");
            log.debug("获取摄像机视频播放地址: {}", url);
            Map<Object, Object> param = new HashMap<>();
            param.put("deviceId", deviceId);
            param.put("videoType", videoType);
            return externalApiService.post(url, param, String.class);
        } catch (GrowUpException e) {
            log.error("获取摄像机视频播放地址: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "长大获取摄像机视频播放地址失败", e.getMessage());
        }
    }

    /**
     * 获取用户资源
     *
     * @return
     */
    @Override
    public JsonNode getRoleAndResource() {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/getRoleAndResource");
            log.debug("获取用户资源: {}", url);
            JsonNode response = externalApiService.post(url, null, JsonNode.class);
            return ConvertNumbers.convertNumbersToString(response);
        } catch (GrowUpException e) {
            log.error("获取用户资源: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "长大获取用户资源接口失败", e.getMessage());
        }
    }

    /**
     * 事件处置卡反馈
     *
     * @param taskId
     * @return
     */
    @Override
    public JsonNode receiveEmergencyTask(String taskId) {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/receiveEmergencyTask");
            log.debug("事件处置卡反馈: {}", url);
            Map<Object, Object> param = new HashMap<>();
            param.put("taskId", taskId);
            JsonNode response = externalApiService.post(url, param, JsonNode.class);
            return ConvertNumbers.convertNumbersToString(response);
        } catch (GrowUpException e) {
            log.error("事件处置卡反馈: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "长大事件处置卡反馈失败", e.getMessage());
        }
    }

    /**
     * 撤销报警
     *
     * @param id
     * @return
     */
    @Override
    public JsonNode revokeAlarm(String id) {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/revokeAlarm");
            log.debug("撤销报警: {}", url);
            Map<Object, Object> param = new HashMap<>();
            param.put("id", id);
            JsonNode response = externalApiService.post(url, param, JsonNode.class);
            return ConvertNumbers.convertNumbersToString(response);
        } catch (GrowUpException e) {
            log.error("撤销报警: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "长大撤销报警失败", e.getMessage());
        }
    }

    /**
     * 获取事件附近摄像机
     *
     * @param eventId
     * @param count
     * @return
     */
    @Override
    public JsonNode getEventNearByCameras(String eventId, Integer count) {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/getEventNearByCameras");
            log.debug("获取事件附近摄像机: {}", url);
            Map<Object, Object> param = new HashMap<>();
            param.put("eventId", eventId);
            param.put("count", count);
            JsonNode response = externalApiService.post(url, param, JsonNode.class,SessionContext.getSession().getUserId());
            return ConvertNumbers.convertNumbersToString(response);
        } catch (GrowUpException e) {
            log.error("获取事件附近摄像机: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "长大获取事件附近摄像机失败", e.getMessage());
        }
    }

    /**
     * 查询用户上报记录
     *
     * @param param
     * @return
     */
    @Override
    public JsonNode getUserReportRecordList(GrowUpBaseParam param) {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/getUserReportRecordList");
            log.debug("查询用户上报记录: {}", url);
            JsonNode response = externalApiService.post(url, param, JsonNode.class);
            return ConvertNumbers.convertNumbersToString(response);
        } catch (GrowUpException e) {
            log.error("查询用户上报记录: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "长大查询用户上报记录失败", e.getMessage());
        }
    }

    /**
     * 通过token获取用户id
     *
     * @param token
     * @return
     */
    @Override
    public String getUserIdByToken(String token) {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/getUserIdByToken?token=" + token);
            log.debug("通过token获取用户id: {}", url);
            return externalApiService.post(url, null, String.class);
        } catch (GrowUpException e) {
            log.error("通过token获取用户id: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "长大通过token获取用户id失败", e.getMessage());
        }
    }

    /**
     * 查询审批单
     *
     * @param id
     * @return
     */
    @Override
    public JsonNode approveDetail(String id) {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/approve/detail");
            log.debug("查询审批单: {}", url);
            Map<Object, Object> param = new HashMap<>();
            param.put("id", id);
            JsonNode response = externalApiService.post(url, param, JsonNode.class);
            return ConvertNumbers.convertNumbersToString(response);
        } catch (GrowUpException e) {
            log.error("查询审批单: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "长大查询审批单失败", e.getMessage());
        }
    }

    /**
     * 获取车载终端列表
     *
     * @return
     */
    @Override
    public JsonNode getVehicleWithVideoList() {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/getVehicleWithVideoList");
            log.debug("获取车载终端列表: {}", url);
            JsonNode response = externalApiService.post(url, null, JsonNode.class);
            return ConvertNumbers.convertNumbersToString(response);
        } catch (GrowUpException e) {
            log.error("获取车载终端列表: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "长大获取车载终端列表失败", e.getMessage());
        }
    }

    /**
     * 获取车辆实时位置
     *
     * @param ids
     * @return
     */
    @Override
    public JsonNode getVehiclePosition(List<String> ids) {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/getVehiclePosition");
            log.debug("获取车辆实时位置: {}", url);
            Map<Object, Object> param = new HashMap<>() {{
                put("ids", ids);
            }};
            JsonNode response = externalApiService.post(url, param, JsonNode.class);
            return ConvertNumbers.convertNumbersToString(response);
        } catch (GrowUpException e) {
            log.error("获取车辆实时位置: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "长大获取车辆实时位置失败", e.getMessage());
        }
    }

    /**
     * 审批通过
     *
     * @param param
     * @return
     */
    @Override
    public JsonNode approveAccess(PublishApproveChange param) {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/approve/access");
            log.debug("审批通过: {}", url);
            JsonNode response = externalApiService.post(url, param, JsonNode.class);
            return ConvertNumbers.convertNumbersToString(response);
        } catch (GrowUpException e) {
            log.error("审批通过: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "长大审批通过失败", e.getMessage());
        }
    }

    /**
     * 审批拒绝
     *
     * @param param
     * @return
     */
    @Override
    public JsonNode approveReject(PublishApproveChange param) {
        try {
            String url = buildUrl("/SubCenterMonitor/dispatchApi/approve/reject");
            log.debug("审批拒绝: {}", url);
            JsonNode response = externalApiService.post(url, param, JsonNode.class);
            return ConvertNumbers.convertNumbersToString(response);
        } catch (GrowUpException e) {
            log.error("审批拒绝: {}", e.getMessage(), e);
            throw new GrowUpException(ResultCode.PROGRAM_ERROR.getCode(), "长大审批拒绝失败", e.getMessage());
        }
    }

}
